import { exampleAppRequestClient } from '#/api/request';
import { useUserStore } from '@vben/stores';

// 获取当前用户的tenantId
const getTenantId = (): string => {
  const userStore = useUserStore();
  console.debug('getTenantId', userStore.userInfo);
  const tenantId = userStore.userInfo?.tenantId;
  console.debug('getTenantId', tenantId);
  if (!tenantId) {
    throw new Error('未获取到租户ID，请重新登录');
  }
  return tenantId;
};

// 示例应用数据类型定义
export interface ExampleAppItem {
  id: string;
  tenant_id: string;
  name: string;
  code: string;
  domain?: string;
  is_active: boolean;
  created_at: string;
  updated_at: string;
  deleted_at?: string;
  deleted: boolean;
}

// 示例应用列表查询参数
export interface ExampleAppListParams {
  page: number;
  size: number;
  name?: string;
  code?: string;
  is_active?: boolean;
  domain?: string;
  tenant_id?: string;
}

// 示例应用列表响应结果
export interface ExampleAppListResult {
  items: ExampleAppItem[];
  total: number;
  page: number;
  size: number;
}

// 创建示例应用请求参数
export interface CreateExampleAppRequest {
  name: string;
  code: string;
  domain?: string;
  is_active?: boolean;
  tenant_id: string;
}

// 更新示例应用请求参数
export interface UpdateExampleAppRequest extends CreateExampleAppRequest {
  id: string;
}

// 示例应用API接口
export const exampleAppApi = {
  // 获取示例应用列表
  getList: (params: ExampleAppListParams): Promise<ExampleAppListResult> => {
    const tenantId = getTenantId();
    return exampleAppRequestClient.get(`/api/v1/tenants/${tenantId}/apps`, { params });
  },

  // 获取示例应用详情
  getDetail: (id: string): Promise<ExampleAppItem> => {
    const tenantId = getTenantId();
    return exampleAppRequestClient.get(`/api/v1/tenants/${tenantId}/apps/${id}`);
  },

  // 创建示例应用
  create: (data: CreateExampleAppRequest): Promise<ExampleAppItem> => {
    const tenantId = getTenantId();
    return exampleAppRequestClient.post(`/api/v1/tenants/${tenantId}/apps`, data);
  },

  // 更新示例应用
  update: (data: UpdateExampleAppRequest): Promise<ExampleAppItem> => {
    const tenantId = getTenantId();
    return exampleAppRequestClient.put(`/api/v1/tenants/${tenantId}/apps/${data.id}`, data);
  },

  // 删除示例应用（逻辑删除）
  delete: (id: string): Promise<void> => {
    const tenantId = getTenantId();
    return exampleAppRequestClient.delete(`/api/v1/tenants/${tenantId}/apps/${id}`);
  },

  // 批量删除示例应用
  batchDelete: (ids: string[]): Promise<void> => {
    const tenantId = getTenantId();
    return exampleAppRequestClient.delete(`/api/v1/tenants/${tenantId}/apps/batch`, { data: { ids } });
  },

  // 启用/禁用示例应用
  toggleStatus: (id: string, is_active: boolean): Promise<ExampleAppItem> => {
    const tenantId = getTenantId();
    return exampleAppRequestClient.put(`/api/v1/tenants/${tenantId}/apps/${id}/status`, { is_active });
  },

  // 检查示例应用编码是否可用
  checkCode: (code: string, excludeId?: string): Promise<{ available: boolean }> => {
    const tenantId = getTenantId();
    return exampleAppRequestClient.get(`/api/v1/tenants/${tenantId}/apps/check-code`, { 
      params: { code, exclude_id: excludeId } 
    });
  },

  // 恢复已删除的示例应用
  restore: (id: string): Promise<ExampleAppItem> => {
    const tenantId = getTenantId();
    return exampleAppRequestClient.put(`/api/v1/tenants/${tenantId}/apps/${id}/restore`);
  },

  // 永久删除示例应用
  forceDelete: (id: string): Promise<void> => {
    const tenantId = getTenantId();
    return exampleAppRequestClient.delete(`/api/v1/tenants/${tenantId}/apps/${id}/force`);
  },
};